﻿/*
 * 由SharpDevelop创建。
 * 用户： zhwang
 * 日期: 2018/1/19
 * 时间: 11:55
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
 
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Microsoft.Win32;


namespace zhwang2008
{
    /// <summary>
    /// 通过zhwang命令设置自动加载程序，unzhwang取消自动加载
    /// </summary>
    public class AutoLoad
    {
        public AutoLoad()
        {
        }

        //来自他人成果代码
        [CommandMethod("zhwang")]
        public void RegisterMyApp()
        {
            // 获取AutoCAD的Applications键

            // 2012版使用RegistryProductRootKey属性

            // 2014版使用MachineRegistryProductRootKey属性

            //string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;


#if CadVersion08
												
		string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
												
#elif CadVersion13

            string sProdKey = HostApplicationServices.Current.MachineRegistryProductRootKey;

#endif


            string sAppName = "MyApp";

            RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);

            RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);

            // 检查”MyApp”键是否存在

            string[] subKeys = regAcadAppKey.GetSubKeyNames();

            foreach (string subKey in subKeys)
            {
                // 如果应用程序已经注册，就退出

                if (subKey.Equals(sAppName))
                {
                    regAcadAppKey.Close();

                    return;
                }
            }
            // 获取本模块的位置（注册本程序自己）

            string sAssemblyPath = Assembly.GetExecutingAssembly().Location;

            // 注册应用程序

            RegistryKey regAppAddInKey = regAcadAppKey.CreateSubKey(sAppName);

            regAppAddInKey.SetValue("DESCRIPTION", sAppName, RegistryValueKind.String);

            regAppAddInKey.SetValue("LOADCTRLS", 14, RegistryValueKind.DWord);

            regAppAddInKey.SetValue("LOADER", sAssemblyPath, RegistryValueKind.String);

            regAppAddInKey.SetValue("MANAGED", 1, RegistryValueKind.DWord);

            regAcadAppKey.Close();





        }
        [CommandMethod("unzhwang")]
        public void UnregisterMyApp()
        {
            // 获取AutoCAD的Applications键

            //string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;

#if CadVersion08
												
		string sProdKey = HostApplicationServices.Current.RegistryProductRootKey;
												
#elif CadVersion13

            string sProdKey = HostApplicationServices.Current.MachineRegistryProductRootKey;

#endif


            string sAppName = "MyApp";

            try
            {

                RegistryKey regAcadProdKey = Registry.CurrentUser.OpenSubKey(sProdKey);

                RegistryKey regAcadAppKey = regAcadProdKey.OpenSubKey("Applications", true);

                // 删除应用程序的注册表键

                regAcadAppKey.DeleteSubKeyTree(sAppName);

                regAcadAppKey.Close();


            }
            catch (System.Exception)
            {

                throw;
            }

        }

    }

}
